#################################
# Online Appendix G
# Experiment 4
#################################

###Log
my_log <- file("appendix_OG_log.txt")

sink(my_log, append=T, type="output")
sink(my_log, append=T, type="message")

cat(readChar(rstudioapi::getSourceEditorContext()$path, # Writing currently opened R script to file
             file.info(rstudioapi::getSourceEditorContext()$path)$size))

start_time <- Sys.time()
start_time

###Packages

library(rio)
library(tidyverse)
library(ggstance)

######################################
# Loading marginal effect estimates
# from stata
######################################

clean_func <- function(x) {
  x %>%
    dplyr::rename(ame = `_margin`, 
                  pvalue = `_pvalue`, 
                  lower = `_ci_lb`, 
                  upper = `_ci_ub`, 
                  change =`_by1`, 
                  treat = `_deriv`) %>%
    dplyr::select(ame, pvalue, lower, upper, change, treat)
}

###support
support <- import("./Online Appendix G/support01.dta")
support <- clean_func(support) %>%
  mutate(dv = "Policy Support")

###argdiff
argdiff <- import("./Online Appendix G/argdiff01.dta")
argdiff <- clean_func(argdiff) %>%
  mutate(dv = "Argument Difference")

###inferences
inferences <- import("./Online Appendix G/inferences.dta")
inferences <- clean_func(inferences) %>%
  mutate(dv = "Inferences")

###prox
prox01 <- import("./Online Appendix G/prox01.dta")
prox01 <- clean_func(prox01) %>%
  mutate(dv = "Proximity")

###motives
motives <- import("./Online Appendix G/motives.dta")
motives <- clean_func(motives) %>%
  mutate(dv = "Motives")

###inparty
in01 <- import("./Online Appendix G/in01.dta")
in01 <- clean_func(in01) %>%
  mutate(dv = "In-Party Therm")

###Bind
comb <- bind_rows(support, argdiff, inferences, 
                  prox01, motives, in01) %>%
  mutate(sig = if_else(pvalue < 0.05, "Significant < 0.05", 
                       "Insignificant p >= 0.05"))

###Figure
comb %>%
  mutate(treat = factor(treat, 
                        levels=c(4,3,2), 
                        labels=c("Insinuation", "Cue w/Insinuation", 
                                 "Party Cue")), 
         change = factor(change, 
                         levels=c(0,1), 
                         labels=c("Stereotypical", "Counter-Stereotypical")), 
         dv = factor(dv, levels=c("Policy Support", "Argument Difference", 
                                  "Inferences", "Proximity", 
                                  "Motives", "In-Party Therm"))) %>%
  ggplot(aes(x=ame, y=treat, shape=sig)) + 
  geom_pointrangeh(aes(xmin=lower, xmax=upper)) + 
  facet_grid(dv ~ change) + 
  geom_vline(xintercept=0, linetype="dashed", color="red") + 
  theme_bw() + 
  labs(shape = "Statistically Significant?", 
       y = NULL, 
       x = "AME") + 
  theme(legend.position = "bottom", 
        strip.text.y = element_text(angle = 0)) + 
  scale_shape_manual(values=c(1,16))

ggsave("./Online Appendix G/figure_og1.png", 
       height=8, width=14)


end_time <- Sys.time()
end_time

closeAllConnections()